<template>
  <a-modal :maskClosable="false"
    :title='title'
    :width='800'
    :visible='visible'
    :confirmLoading='confirmLoading'
    @ok='handleOk'
    @cancel='handleCancel'
    cancelText='关闭'>

    <a-spin :spinning='confirmLoading'>
      <a-form-model ref='form' v-bind='layout' :model='model' :rules='validatorRules'>
        <a-form-model-item label='服务类型' required prop='serviceType'>
          <a-select v-model='model.serviceType' placeholder='请选择服务类型' default-value='1'>
            <a-select-option value='0'>医生</a-select-option>
            <a-select-option value='1'>护士</a-select-option>
            <a-select-option value='2'>护工</a-select-option>
            <a-select-option value='3'>社工</a-select-option>
            <a-select-option value='4'>其他</a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label='服务名称' required prop='projectName'>
          <a-input placeholder='请输入服务名称' v-model='model.projectName' />
        </a-form-model-item>
        <a-form-model-item label='服务解决目标' required prop='projectTarget'>
          <a-textarea style='height: 200px' placeholder='请输入服务解决目标' v-model='model.projectTarget' />
        </a-form-model-item>
        <a-form-model-item label='服务天数' required prop='totalDay'>
          <a-input placeholder='请输入服务天数' v-model='model.totalDay' />
        </a-form-model-item>
        <a-form-model-item label='每天服务次数' required prop='dayFrequency'>
          <a-input placeholder='请输入每天服务次数' v-model='model.dayFrequency' />
        </a-form-model-item>
        <a-form-model-item label='间隔天数' required prop='partDay'>
          <a-input placeholder='请输入间隔天数 0代表不间隔' v-model='model.partDay' />
        </a-form-model-item>
        <a-form-model-item label='备注'  prop='remark'>
          <a-textarea style='height: 200px' placeholder='请输入备注' v-model='model.remark' />
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import { httpAction, putAction, getAction, postAction } from '@/api/manage'

export default {
  name: 'hHisEvaluationServiceProjectModal',
  data() {
    return {
      title: '操作',
      visible: false,
      model: {},
      layout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 14 }
      },
      confirmLoading: false,
      validatorRules: {
        serviceType: [{ required: true, message: '请输入服务类型!' }],
        totalDay: [{ required: true, message: '请输入服务天数!' }],
        dayFrequency: [{ required: true, message: '请输入每天服务次数!' }],
        partDay: [{ required: true, message: '请输入间隔天数!' }],
      },
      url: {
        add: '/ch/evaluation/project',
        edit: '/ch/evaluation/project'
      }
    }
  },
  created() {
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    close() {
      this.$refs.form.clearValidate()
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
            that.close()
          })
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.close()
    }


  }
}
</script>

<style scoped>

</style>